package com.sunfield.windpower.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Created by wxl on 2017/5/31.
 */

public class FileDownloadService {
    FileDownloadDBHeloper dbHeloper;

    public FileDownloadService(Context context) {
        dbHeloper = new FileDownloadDBHeloper(context);
    }

    public void addDownloadInfo(long downloadId, String url){
        SQLiteDatabase db = dbHeloper.getWritableDatabase();
        db.beginTransaction();
        try {
            db.execSQL("insert into download_info (download_id, file_url) values(?, ?)", new Object[]{downloadId, url});
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
        db.close();
    }

    public long getDownloadId(String url){
        long downloadId = -1L;
        SQLiteDatabase db = dbHeloper.getReadableDatabase();
        Cursor c = db.rawQuery("select download_id from download_info where file_url = ?", new String[]{url});
        if(c.moveToFirst()){
            downloadId = c.getLong(0);
        }
        db.close();
        return downloadId;
    }

}
